package com.xxl.sso.server.schedule;

import com.xxl.sso.server.core.result.ReturnT;
import com.xxl.sso.server.service.SysCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @ClassName CacheEvictionScheduler
 * @Description mysql缓存过期清除定时任务
 * @Author wangke
 * @Date 2021/8/25 10:39
 */
@Slf4j
@Component
public class CacheEvictionScheduler {

    @Autowired
    private SysCacheService sysCacheService;

    @Value("${xxl.sso.mysqlSubstitute:false}")
    private boolean mysqlSubstitute;

    //每隔5分钟清除一次过期缓存
    @Scheduled(initialDelay = (60 * 1000),  fixedRate = (5 * 60 * 1000))
    public void removeExpiredCache(){
        if(mysqlSubstitute){
            log.debug("定时任务CacheEvictionScheduler removeExpiredCache() executed.");
            ReturnT<Boolean> result = sysCacheService.deleteExpiredSysCache();
            if(ReturnT.FAIL_CODE != result.getCode() && result.getData()){
                log.info("定时任务CacheEvictionScheduler removeExpiredCache() successfully executed.");
            }else{
                log.error("定时任务CacheEvictionScheduler removeExpiredCache() failed to execute.");
            }
        }
    }

}
